package top.codedance.iotp.server.net.handle.listenner;

import cd.io.netty.channel.ChannelFuture;
import cd.io.netty.channel.ChannelFutureListener;
import top.codedance.iotp.server.ServerApplication;
import top.codedance.iotp.server.net.TcpServer;

import java.util.*;

public class TcpConnectionListener implements ChannelFutureListener {

    public void operationComplete(final ChannelFuture future) {
        if (future.isSuccess() && future.channel().isActive()) {
            Timer timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    if(!future.channel().isActive()){
                        timer.cancel();
                    }
                    String[] clientDeviceIds = TcpServer.sessionManager.getClientDeviceIds();
                    ArrayList<String> cdis = new ArrayList<>(Arrays.asList(clientDeviceIds));
                    Set<String> strings = ServerApplication.clientsLoadInfo.keySet();
                    for (String did : strings) {
                        if(!cdis.contains(did)) {
                            ServerApplication.clientsLoadInfo.remove(did);
                        }
                    }
                }
            }, 1000l * 10, 1000l * 10);
        }
    }
}
